﻿// -----------------------------------------------------------------------
// <copyright file="ErrorHandlerControllerBase.cs">
// Copyright (c) 2013-2013 Andrey Veselov. All rights reserved.
// License:  Microsoft Public License (MS-PL)
// Contacts: http://andrey.moveax.com  andrey@moveax.com
// </copyright>
// -----------------------------------------------------------------------

namespace Moveax.Mvc.ErrorHandler
{
    using System.Web.Mvc;

    public abstract class ErrorHandlerControllerBase : Controller
    {
        public abstract ActionResult Default(ErrorDescription errorDescription);

        protected abstract void HandleError(ErrorDescription errorDescription);

        protected override void HandleUnknownAction(string actionName)
        {
            var error = this.RouteData.Values["errorDescription"] as ErrorDescription;
            this.HandleError(error);

            this.Default(error).ExecuteResult(this.ControllerContext);
        }
        
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            var error = filterContext.ActionParameters["errorDescription"] as ErrorDescription;
            this.HandleError(error);
        }
    }
}